package phantomread;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Transaction2 {

    private static final String URL = "jdbc:mysql://localhost:3306/transaction_demo";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        try {;
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            conn.setAutoCommit( false);
            conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
            Statement stmt = conn.createStatement();
            ResultSet resultSet = stmt.executeQuery("select * from account");
            while (resultSet.next()) {
                System.out.println("用户ID:" +resultSet.getInt(1) + ", 用户名：" + resultSet.getString(2) + ", 余额：" + resultSet.getDouble(3));
            }

            ResultSet resultSet2 = stmt.executeQuery("select * from account for update");
            while (resultSet2.next()) {
                System.out.println("用户ID:" +resultSet2.getInt(1) + ", 用户名：" + resultSet2.getString(2) + ", 余额：" + resultSet2.getDouble(3));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
